package io.renren.modules.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.page.PageData;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.modules.health.dao.PostsDao;
import io.renren.modules.health.dto.PostsDTO;
import io.renren.modules.health.entity.PostsEntity;
import io.renren.modules.health.service.PostsService;
import io.renren.modules.security.user.SecurityUser;
import io.renren.modules.sys.dto.SysUserDTO;
import io.renren.modules.sys.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 帖子表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2024-05-09
 */
@Service
public class PostsServiceImpl extends CrudServiceImpl<PostsDao, PostsEntity, PostsDTO> implements PostsService {

    @Autowired
    private SysUserService sysUserService;

    @Override
    public QueryWrapper<PostsEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");
        String title = (String)params.get("title");
        Object creator = params.get("creator");

        QueryWrapper<PostsEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);
        wrapper.like(StringUtils.isNotBlank(title), "title", title);
        if (creator != null) {
            wrapper.eq("creator", Long.valueOf(creator.toString()));
        }

        return wrapper;
    }

    @Override
    public PageData<PostsDTO> pagePosts(Map<String, Object> params) {
        // 获取当前登录用户
        Long userId = SecurityUser.getUserId();
        params.put("creator", userId);
        PageData<PostsDTO> page = this.page(params);
        page.getList().forEach(e -> {
            SysUserDTO creatorDto = sysUserService.get(e.getCreator());
            e.setCreatorName(creatorDto == null ? "" : creatorDto.getUsername());
            SysUserDTO updaterDto = sysUserService.get(e.getUpdater());
            e.setUpdaterName(creatorDto == null ? "" : updaterDto.getUsername());
        });
        return page;
    }

    @Override
    public List<PostsDTO> listPosts(Map<String, Object> params) {
        List<PostsDTO> list = this.list(params);
        list.forEach(e -> {
            SysUserDTO creatorDto = sysUserService.get(e.getCreator());
            e.setCreatorName(creatorDto == null ? "" : creatorDto.getUsername());
            SysUserDTO updaterDto = sysUserService.get(e.getUpdater());
            e.setUpdaterName(creatorDto == null ? "" : updaterDto.getUsername());
        });
        return list;
    }

    @Override
    public PostsDTO getPosts(Long id) {
        PostsDTO postsDTO = this.get(id);
        SysUserDTO creatorDto = sysUserService.get(postsDTO.getCreator());
        postsDTO.setCreatorName(creatorDto == null ? "" : creatorDto.getUsername());
        SysUserDTO updaterDto = sysUserService.get(postsDTO.getUpdater());
        postsDTO.setUpdaterName(creatorDto == null ? "" : updaterDto.getUsername());
        return postsDTO;
    }

}